Imports ccapps.persistent.CaseTracker

Public Class CaseDetail
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblContextHeader As System.Web.UI.WebControls.Label
    Protected WithEvents lblCaseType As System.Web.UI.WebControls.Label
    Protected WithEvents lblCaseItemID As System.Web.UI.WebControls.Label
    Protected WithEvents lblItemID As System.Web.UI.WebControls.Label
    Protected WithEvents lblArea As System.Web.UI.WebControls.Label
    Protected WithEvents lblEstimatedTime As System.Web.UI.WebControls.Label
    Protected WithEvents lblDescription As System.Web.UI.WebControls.Label
    Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
    Protected WithEvents lblReason As System.Web.UI.WebControls.Label
    Protected WithEvents lblProductionDate As System.Web.UI.WebControls.Label
    Protected WithEvents lblRelease As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Lblok As System.Web.UI.WebControls.Label
    Protected WithEvents ContentPaneFooter1 As Library.WebControls.ContentPaneFooter
    Protected WithEvents txtNotes As System.Web.UI.WebControls.TextBox
    Protected WithEvents trNotes As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trApproval As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents rlstApproval As System.Web.UI.WebControls.RadioButtonList
    Protected WithEvents ddlStatus As System.Web.UI.WebControls.DropDownList
    Protected WithEvents trCaseStatus As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents lblGroup As System.Web.UI.WebControls.Label
    Protected WithEvents rfvAttachment As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents trAttachment As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents fuAttachment As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents Table3 As System.Web.UI.HtmlControls.HtmlTable
    Protected WithEvents tblCaseSummary As System.Web.UI.HtmlControls.HtmlTable

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private ReadOnly Property CaseID() As String
        Get
            Return Me.Request.QueryString("CaseID")
        End Get
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim caseInfo As DataRow = persistent.CaseTracker.GetCase(Me.CaseID)

        If Not persistent.CaseTracker.IsUserAuthorized(Session("user"), AuthorizationLevel.View, caseInfo("UserGroup")) Then
            Page.Controls.Clear()
            Response.Write("You are not authorized to view this page!")
            Return
        End If


        Me.PageTitle = "Case Detail"
        Me.lblContextHeader.Text = "Case Detail. Case Number : " & Me.CaseID


        If Not Me.IsPostBack Then

            Me.loadCaseInfo(caseInfo)

        End If
    End Sub

    Private Sub loadCaseInfo(ByVal caseInfo As DataRow)
        Me.ddlStatus.Items.Clear()
        For Each item As ListItem In persistent.CaseTracker.GetListItemsFromEnum(GetType(persistent.CaseTracker.CaseStates))
            Me.ddlStatus.Items.Add(item)
        Next

        Me.lblGroup.Text = persistent.CaseTracker.GetListItemsFromEnum(GetType(persistent.CaseTracker.UserGroups)).FindByValue(caseInfo("UserGroup")).Text
        Me.lblCaseType.Text = caseInfo("CASETYPE")
        Me.lblCaseItemID.Text = caseInfo("CASETYPE") & " ID"
        Me.lblItemID.Text = caseInfo("CaseItemID")
        Me.lblArea.Text = caseInfo("AREA")
        Me.lblEstimatedTime.Text = caseInfo("ESTIMATEDTIME")
        Me.lblDescription.Text = caseInfo("DESCRIPTION")
        Me.lblStatus.Text = caseInfo("CASESTATUS")
        Me.lblReason.Text = caseInfo("REASON")
        Me.lblProductionDate.Text = caseInfo("PRODUCTIONDATE")
        Me.lblRelease.Text = caseInfo("RELEASE")
        Me.ddlStatus.SelectedValue = caseInfo("CASESTATUS")
        If caseInfo("CaseStatus") <> "On-Approval" Then Me.ddlStatus.Items.RemoveAt(0)

        Me.DataGrid1.DataSource = persistent.CaseTracker.GetCaseHistory(Me.CaseID)
        Me.DataGrid1.DataBind()

        Me.trCaseStatus.Visible = (caseInfo("CaseStatus") <> "Closed" And caseInfo("CaseStatus") <> "Cancelled" And caseInfo("CaseStatus") <> "On-Approval") AndAlso persistent.CaseTracker.IsUserAuthorized(Session("user"), AuthorizationLevel.ViewAndCreate, caseInfo("UserGroup"))
        Me.trAttachment.Visible = Me.trCaseStatus.Visible
        Me.trApproval.Visible = caseInfo("CaseStatus") = "On-Approval" AndAlso persistent.CaseTracker.IsUserAuthorized(Session("user"), AuthorizationLevel.ViewCreateAndApprove, caseInfo("UserGroup"))
        Me.trNotes.Visible = Me.trCaseStatus.Visible OrElse Me.trApproval.Visible
        Me.ContentPaneFooter1.ConfirmButton.Enabled = Me.trNotes.Visible

    End Sub

    Private Sub ContentPaneFooter1_Confirmed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1.Confirmed
        Try
            Dim caseInfo As DataRow = persistent.CaseTracker.GetCase(Me.CaseID)
            If caseInfo("CaseStatus") = "On-Approval" Then
                Dim approversVote As VoteType = CType(CInt(Me.rlstApproval.SelectedValue), VoteType)
                persistent.CaseTracker.VoteCase(Me.CaseID, approversVote, Me.txtNotes.Text, Session("user"))
                If approversVote = VoteType.Approved Then Me.sendInformMail(caseInfo)
                Me.Lblok.Text = "Case is " & approversVote.ToString() & "."
            Else
                Dim attachmentsPath As String = Server.MapPath("~/CaseTrackerAttachments/")
                Dim fileInfo As IO.FileInfo
                Dim fileNameQualifier As Integer = 0
                Dim savedFileName As String = String.Empty

                If Not (Me.fuAttachment.PostedFile Is Nothing OrElse Me.fuAttachment.PostedFile.FileName = String.Empty) Then
                    fileInfo = New IO.FileInfo(attachmentsPath & Me.fuAttachment.PostedFile.FileName.Substring(Me.fuAttachment.PostedFile.FileName.LastIndexOf("\") + 1))
                    Dim fileName As String = Me.fuAttachment.PostedFile.FileName.Substring(Me.fuAttachment.PostedFile.FileName.LastIndexOf("\") + 1).Replace(fileInfo.Extension, "")
                    While fileInfo.Exists
                        fileNameQualifier += 1
                        fileInfo = New IO.FileInfo(attachmentsPath & fileName & "[" & fileNameQualifier.ToString() & "]" & fileInfo.Extension)
                    End While

                    Me.fuAttachment.PostedFile.SaveAs(fileInfo.FullName)
                    savedFileName = fileInfo.Name
                End If

                persistent.CaseTracker.AddCaseHistory(Me.CaseID, Session("user"), Me.ddlStatus.SelectedValue, savedFileName, Me.txtNotes.Text)
                Me.Lblok.Text = "Case status is changed."
            End If
            Me.txtNotes.Text = ""
            loadCaseInfo(persistent.CaseTracker.GetCase(Me.CaseID))
        Catch ex As Exception
            Me.Lblok.Text = "Exception occured : " & ex.Message
        End Try
    End Sub

    Private Sub sendInformMail(ByVal caseInfo As DataRow)
        Dim mailMessage As New System.Web.Mail.MailMessage
        mailMessage.From = "billingnet@avea.com.tr"
        mailMessage.To = persistent.CaseTracker.GetInformTarget(CType(CInt(caseInfo("UserGroup")), persistent.CaseTracker.UserGroups))
        mailMessage.Subject = "BillingNet Case Tracker"
        mailMessage.BodyFormat = Mail.MailFormat.Html
        Dim stringWriter As New System.IO.StringWriter
        Dim textWriter As New HtmlTextWriter(stringWriter)
        Me.tblCaseSummary.RenderControl(textWriter)

        mailMessage.Body = stringWriter.ToString().Replace("On-Approval", "Open")

        If mailMessage.To.Trim().Length > 0 Then
            System.Web.Mail.SmtpMail.SmtpServer = "10.1.240.133"
            System.Web.Mail.SmtpMail.Send(mailMessage)
        End If
    End Sub
End Class
